package com.jumi.microservice.common.wdt;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jumi.microservice.common.exception.ExceptionEnum;
import com.jumi.microservice.material.dto.JmGoodsRequest;
import com.jumi.microservice.material.dto.JmMaterialRequest;

import java.io.IOException;
import java.util.*;

public class WdtUtils {


    /**
     * 根据商品编码查询商品信息
     *
     * @param goodsNo
     * @return
     */
    public static JSONObject goodsQuery(String goodsNo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("goods_no", goodsNo);
        String url = "goods_query.php";
        return toWdt(url, params);
    }

    /**
     * 货品查询
     * 一次查询最大值为100
     *
     * @param startTime
     * @param endTime
     * @return JsonObject
     */
    public static JSONObject goodsListQuery(String startTime, String endTime, int pageNum, int pageSize) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("start_time", startTime);
        params.put("end_time", endTime);
        params.put("page_no", pageNum);
        params.put("page_size", pageSize);
        String url = "goods_query.php";
        return toWdt(url, params);
    }


    /**
     * 货品档案推送
     *
     * @param jmGoodsRequests
     * @return
     */
    public static JSONObject goodsPush(List<JmGoodsRequest> jmGoodsRequests) {

        List<Map<String, Object>> goodsList = new ArrayList<>();
        for (JmGoodsRequest jmGoods : jmGoodsRequests) {
            List<JmMaterialRequest> jmMaterials = jmGoods.getMaterials();
            List<Map<String, Object>> specList = new ArrayList<>();
            for (JmMaterialRequest jmMaterial : jmMaterials) {
                Map<String, Object> spec = new HashMap<>();
                spec.put("spec_no", jmMaterial.getMaterialCode());
                spec.put("spec_name", jmMaterial.getSpecName());
                spec.put("weight", jmMaterial.getWeight());
                specList.add(spec);
            }
            Map<String, Object> goods = new HashMap<>();
            goods.put("goods_no", jmGoods.getGoodsCode());
            goods.put("goods_type", "1");
            goods.put("goods_name", jmGoods.getGoodsName());
            goods.put("spec_list", specList);
            goodsList.add(goods);
        }


        //通过第三方json解析工具类fastjson将map解析成json
        String goods_list_json = JSON.toJSONString(goodsList);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("goods_list", goods_list_json);
        return toWdt("goods_push.php", params);
    }


    /**
     * 采购单创建
     *
     * @param details
     * @return
     */
    public static JSONObject purchasePush(List<Map<String, Object>> details) {

        Map<String, Object> purchaseInfo = new HashMap<>();
        purchaseInfo.put("provider_no", "001");
        //正式数据Jumi
        purchaseInfo.put("warehouse_no", "shunyuan2-test");
        String outerNo = createOuterNo();
        purchaseInfo.put("outer_no", outerNo);
        JSONArray details_list = new JSONArray();
        for (Map<String, Object> detail : details) {
            JSONObject obj = new JSONObject();
            obj.put("spec_no", detail.get("material_code"));
            obj.put("num", detail.get("in_num"));
            obj.put("price", 0);
            obj.put("remark", "API接口申请");
            details_list.add(obj);
        }
        purchaseInfo.put("details_list", details_list);
        String purchase_info_json = JSON.toJSONString(purchaseInfo);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("purchase_info", purchase_info_json);
        return toWdt("purchase_order_push.php", params);
    }

    /**
     * 根据单号查询入库申请单
     *
     * @param purchaseCode
     * @return
     */
    public static JSONObject purchaseQuery(String purchaseCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("purchase_no", purchaseCode);
        String url = "purchase_order_query.php";
        return toWdt(url, params);
    }

    /**
     * 根据时间段产线入库申请
     * 查询范围30天之内
     * pageSize = [1,100]
     *
     * @param startTime
     * @param endTime
     * @param pageNum
     * @param pageSize
     * @return
     */
    public static JSONObject purchaseListQuery(String startTime, String endTime, int pageNum, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("start_time", startTime);
        params.put("end_time", endTime);
        params.put("page_no", pageNum);
        params.put("page_size", pageSize);
        String url = "purchase_order_query.php";
        return toWdt(url, params);
    }

    private static String createOuterNo() {
        StringBuffer sb = new StringBuffer("CG");
        Date date = new Date();
        String noEnd = DateUtil.format(date, "yyyyMMddHHmmss");
        return sb.append(noEnd).toString();
    }


    /**
     * 根据relativeUrl,params向旺店通推送数据
     *
     * @param relativeUrl
     * @param params
     * @return
     */
    private static JSONObject toWdt(String relativeUrl, Map<String, Object> params) {
        try {
            WdtClient client = new WdtClient();
            String response = client.execute(relativeUrl, params);
            JSONObject json = JSONObject.parseObject(response);
            return json;
        } catch (IOException e) {
            throw ExceptionEnum.FAIL_TO_WDT.newException();
        }
    }

}
